#include "BaseCmd.h"
#include "CmdEnvironment.h"
#include <sstream>
#include <iostream>
using namespace std;

bool CBaseCmd::ExcuteImpl(COMMAND& command)
{
	WnString& cmd = command.command;
	stringstream ss;
	if(CMD_HELP == cmd)
	{
		return ShowHelpInfo(command);
	}
	else if(CMD_ROOT == cmd)
	{
		command.group.clear();
	}
	else if(CMD_EXIT == cmd)
	{
		m_cmdResult = "exit programme.";
		exit(0);
	}
	else
		return false;

	return true;
}

bool CBaseCmd::ShowHelpInfo(COMMAND& command)
{
	if(command.group.empty())
	{
		stringstream ss;
		for(MAP_STR_STR::iterator it = CCmdEnvironment::Instance().m_firstLevelCommands.begin(); it != CCmdEnvironment::Instance().m_firstLevelCommands.end(); ++it)
		{
			WnString command_tag = (*it).first;
			WnString command_explain = (*it).second;

			ss << command_tag << "\t" << command_explain << endl;
		}

		m_cmdResult = ss.str();
		return true;
	}
	return false;
}

bool CBaseCmd::SubExcute(COMMAND& command)
{
	if(NULL != m_ptr_successor)
		return m_ptr_successor->Excute(command);
	else
	{
		cout << "invalid command, press \"?\" for help." << endl << endl;
		return false;
	}
}
